class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        int size = nums.length;
        for(int i = 0; i < size - 2; i++){
            if(nums[i] > 0) break;
            if(i > 0 && nums[i] == nums[i - 1]) continue;   //去重
            int left = i + 1;
            int right = size - 1;
            while(left < right){
                int sum = nums[i] + nums[left] + nums[right];
                if(sum == 0){
                    res.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    while(left < right && nums[left] == nums[left + 1])    left++;  //去重
                    while(left < right && nums[right] == nums[right - 1])   right--;//去重
                    left++;
                    right--;
                }
                else if(sum < 0)  left++;
                else if(sum > 0)  right--;
            }
        }
        return res;
    }
}